package server.main;

import java.awt.BorderLayout;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class MianServer {
	private static JTextArea jta = new JTextArea();
	private static JButton open = new JButton("打开服务器");
	private static JButton clsoe = new JButton("关闭服务器");
	private static ServerSocket serverSocket;
	private static ExecutorService exec;
	private static List<Socket> list = new ArrayList<Socket>();
	static BufferedReader is;
	static PrintWriter os;
	static Socket client = null;

	public static void main(String[] args) {
		serverWindow();
		open.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				startServer();
			}
		});
		clsoe.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				stop();
			}
		});
	}

	public static void stop() {
		try {
			os.close(); // 关闭Socket输出流
			is.close();
			client.close(); // 关闭Socket
			serverSocket.close(); // 关闭ServerSocket
			jta.append("\n服务器已经关闭");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}

	public static void startServer() {
		try {
			serverSocket = new ServerSocket(8889);
			exec = Executors.newCachedThreadPool();

			client = serverSocket.accept();
			list.add(client);
			timer.schedule(task, 0);
		} catch (IOException e) {
			e.printStackTrace();
		}
		jta.append("服务器已经启动");
	}

	static Timer timer = new Timer();
	static TimerTask task = new TimerTask() {

		@Override
		public void run() {
			new Thread(runnable).start();
		}
	};
	static Runnable runnable = new Runnable() {
		
		@Override
		public void run() {
			try {
				System.out.println("----");
				getMessage(client);
			} catch (IOException e) {
				e.printStackTrace();
			}
			timer.schedule(task, 1000);
		}
	};

	private static void getMessage(Socket socket) throws IOException {
		String line;
		is = new BufferedReader(new InputStreamReader(socket.getInputStream()));
		os = new PrintWriter(socket.getOutputStream());
		BufferedReader sin = new BufferedReader(new InputStreamReader(System.in));
		line = sin.readLine();
		System.out.println(line);
		String msg = null;
		while (!line.equals("")) {
			msg = "[" + socket.getPort() + "]说：" + msg;
			jta.append(msg + "\n");
			for (Socket client : list) {
				os.println(client.getOutputStream());
				os.flush();
			}
			line = sin.readLine();
		}
	}

	public static void serverWindow() {
		JFrame frame = new JFrame();
		JPanel jPanel = new JPanel();

		jPanel.add(open);
		jPanel.add(clsoe);
		frame.setTitle("Socket Server Room");
		frame.setLayout(new BorderLayout());
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.add(jta);
		frame.add(jPanel, BorderLayout.SOUTH);
		frame.setVisible(true);
		frame.setSize(300, 200);
		frame.setLocationRelativeTo(null);
	}
}
